package com.ddm.controller.vx;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ddm.context.BaseContext;
import com.ddm.entity.vxEntity.Collection;
import com.ddm.entity.vxEntity.Question;
import com.ddm.exception.DataErrorException;
import com.ddm.result.Result;
import com.ddm.service.vxService.CollectionService;
import com.ddm.service.vxService.QuetsionsService;
import com.ddm.utils.BeanCopyUtil;
import com.ddm.vo.vxVo.IfBookmark;
import com.ddm.vo.vxVo.QuestionVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 题目收藏控制器
 */
@RestController
@RequestMapping("/user/collection")
@Slf4j
public class CollectionController {

    @Autowired
    private CollectionService collectionService;
    
    @Autowired
    private QuetsionsService quetsionsService;

    /**
     * 获取用户收藏的题目列表
     */
    @GetMapping("/questions")
    @ApiOperation("获取用户收藏的题目列表")
    public Result<List<QuestionVO>> getCollectedQuestions() {
        log.info("获取用户收藏的题目列表");
        Long currentUserId = BaseContext.getCurrentId();
        if (currentUserId == null) {
            throw new DataErrorException("用户未登录或登录已过期");
        }
        
        // 1. 获取用户收藏的题目ID列表
        List<Long> questionIds = collectionService.getCollectedQuestionIds(currentUserId);
        
        if (questionIds.isEmpty()) {
            return Result.success(List.of());
        }
        
        // 2. 查询题目详情
        List<Question> questions = quetsionsService.listByIds(questionIds);
        
        // 3. 转换为VO
        List<QuestionVO> questionVOs = BeanCopyUtil.BeanCopyList(questions, QuestionVO.class);
        
        return Result.success(questionVOs);
    }

    /**
     * 取消收藏指定题目
     */
    @DeleteMapping("/{questionId}")
    @ApiOperation("取消收藏指定题目")
    public Result<String> cancelCollection(@PathVariable Long questionId) {
        log.info("取消收藏题目，题目ID: {}", questionId);
        Long currentUserId = BaseContext.getCurrentId();
        if (currentUserId == null) {
            throw new DataErrorException("用户未登录或登录已过期");
        }
        
        if (questionId == null) {
            throw new DataErrorException("题目ID不能为空");
        }
        
        // 执行取消收藏操作（Service层会先检查记录是否存在）
        boolean removed = collectionService.cancelCollection(currentUserId, questionId);
        
        if (removed) {
            return Result.success("取消收藏成功");
        } else {
            return Result.error("取消收藏失败，该题目未收藏或已被取消");
        }
    }

    /**
     * 检查指定题目是否已收藏
     */
    @GetMapping("/check/{questionId}")
    @ApiOperation("检查指定题目是否已收藏")
    public Result<IfBookmark> checkCollectionStatus(@PathVariable Long questionId) {
        log.info("检查题目收藏状态，题目ID: {}", questionId);
        Long currentUserId = BaseContext.getCurrentId();
        if (currentUserId == null) {
            throw new DataErrorException("用户未登录或登录已过期");
        }
        
        if (questionId == null) {
            throw new DataErrorException("题目ID不能为空");
        }
        
        // 调用Service层方法检查收藏状态
        boolean isCollected = collectionService.isQuestionCollected(currentUserId, questionId);
        
        IfBookmark ifBookmark = new IfBookmark();
        if (isCollected) {
            ifBookmark.setIfbookmark(1); // 已收藏
        } else {
            ifBookmark.setIfbookmark(0); // 未收藏
        }
        
        return Result.success(ifBookmark);
    }
} 